package com.chy.blog_web.core.interceptor;

import com.chy.blog_web.core.util.SpaceUserUtil;
import com.chy.blog_web.modular.web.entity.BoSpaceUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 空间登录拦截器
 */
public class SpaceUserLoginInterceptor implements HandlerInterceptor {

    protected Logger logger = LoggerFactory.getLogger(SpaceUserLoginInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler)throws Exception {

        logger.info("--------------------登录拦截器已启动-------------------------");

        HttpSession session = request.getSession(true);
        //获取当前登录用户
        BoSpaceUser spaceUser = (BoSpaceUser)session.getAttribute("spaceUser");
        if(null != spaceUser) {//已登录
            return true;
        }else {//未登录
            //直接重定向到登录页面
            response.sendRedirect(request.getContextPath()+"/blogSpaceLogin");
            return false;
        }
    }
}
